"""
todo                       _oo0oo_
todo                      o8888888o
todo                      88" . "88
todo                      (| -_- |)
todo                      0\  =  /0
todo                    ___/`---'\___
todo                  .' \\|     |// '.
todo                 / \\|||  :  |||// \
todo                / _||||| -:- |||||- \
todo               |   | \\\  - /// |   |
todo               | \_|  ''\---/''  |_/ |
todo               \  .-\__  '-'  ___/-. /
todo             ___'. .'  /--.--\  `. .'___
todo          ."" '<  `.___\_<|>_/___.' >' "".
todo         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
todo         \  \ `_.   \_ __\ /__ _/   .-` /  /
todo     =====`-.____`.___ \_____/___.-`___.-'=====
todo                       `=---='
todo
todo
todo     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
todo
todo           佛祖保佑     永不宕机     永无BUG
"""

"""
+ 一个简单易用的 Python 终端颜色输出库

终端颜色库使用示例

from my_color import *

# 方法1: 使用全局函数
print("=== 使用全局函数 ===")
cprint("这是红色文本", color=RED)
cprint("这是绿色背景的文本", color=WHITE, bg_color=BG_GREEN)
cprint("这是加粗的蓝色文本", color=BLUE, style=BOLD)

# 方法2: 使用快捷方法
print("\n=== 使用快捷方法 ===")
info("这是一条信息")
success("操作成功!")
warning("这是一个警告")
error("发生错误!")
debug("调试信息")
highlight("重要内容!")

# 方法3: 使用类方法
print("\n=== 使用类方法 ===")
TerminalColor.print("黄色文本", color=YELLOW)
TerminalColor.preset("自定义预设文本", "info")

# 方法4: 使用全局实例
print("\n=== 使用全局实例 ===")
tc.print("青色文本", color=CYAN)
tc.success("成功消息")

# 方法5: 获取着色文本但不打印
print("\n=== 获取着色文本 ===")
colored_text = colorize("这是着色的文本", color=MAGENTA, style=UNDERLINE)
print(f"普通文本 + {colored_text} + 普通文本")

# 组合使用
print("\n=== 组合使用 ===")
cprint("红底白字", color=BRIGHT_WHITE, bg_color=BG_RED, style=BOLD)

# 使用预定义颜色常量
print("\n=== 使用颜色常量 ===")
cprint("亮绿色文本", color=BRIGHT_GREEN)
cprint("亮黄色背景", bg_color=BG_BRIGHT_YELLOW)
"""


class TerminalColor:
    """
    终端颜色输出类
    提供了多种颜色和样式，方便在终端中输出彩色文本
    """

    # 颜色代码
    COLORS = {
        # 基础颜色
        "black": 30,
        "red": 31,
        "green": 32,
        "yellow": 33,
        "blue": 34,
        "magenta": 35,
        "cyan": 36,
        "white": 37,
        # 亮色
        "bright_black": 90,
        "bright_red": 91,
        "bright_green": 92,
        "bright_yellow": 93,
        "bright_blue": 94,
        "bright_magenta": 95,
        "bright_cyan": 96,
        "bright_white": 97,
    }

    # 背景颜色代码
    BG_COLORS = {
        "bg_black": 40,
        "bg_red": 41,
        "bg_green": 42,
        "bg_yellow": 43,
        "bg_blue": 44,
        "bg_magenta": 45,
        "bg_cyan": 46,
        "bg_white": 47,
        # 亮背景色
        "bg_bright_black": 100,
        "bg_bright_red": 101,
        "bg_bright_green": 102,
        "bg_bright_yellow": 103,
        "bg_bright_blue": 104,
        "bg_bright_magenta": 105,
        "bg_bright_cyan": 106,
        "bg_bright_white": 107,
    }

    # 样式代码
    STYLES = {
        "reset": 0,
        "bold": 1,
        "dim": 2,
        "italic": 3,
        "underline": 4,
        "blink": 5,
        "reverse": 7,
        "hidden": 8,
        "strikethrough": 9,
    }

    # 预定义组合样式
    PRESETS = {
        "info": {"color": "cyan", "style": "bold"},
        "success": {"color": "green", "style": "bold"},
        "warning": {"color": "yellow", "style": "bold"},
        "error": {"color": "red", "style": "bold"},
        "debug": {"color": "magenta", "style": "dim"},
        "highlight": {
            "color": "bright_yellow",
            "bg_color": "bg_black",
            "style": "bold",
        },
    }

    @classmethod
    def colorize(cls, text, color=None, bg_color=None, style=None):
        """
        为文本添加颜色和样式

        Args:
            text (str): 要着色的文本
            color (str): 文本颜色，可选值见 COLORS
            bg_color (str): 背景颜色，可选值见 BG_COLORS
            style (str): 文本样式，可选值见 STYLES

        Returns:
            str: 着色后的文本
        """
        codes = []

        if style and style in cls.STYLES:
            codes.append(str(cls.STYLES[style]))

        if color and color in cls.COLORS:
            codes.append(str(cls.COLORS[color]))

        if bg_color and bg_color in cls.BG_COLORS:
            codes.append(str(cls.BG_COLORS[bg_color]))

        if codes:
            return f"\033[{';'.join(codes)}m{text}\033[0m"
        else:
            return text

    @classmethod
    def print(cls, text, color=None, bg_color=None, style=None, **kwargs):
        """
        打印带颜色的文本

        Args:
            text (str): 要打印的文本
            color (str): 文本颜色
            bg_color (str): 背景颜色
            style (str): 文本样式
            **kwargs: 其他传递给 print 函数的参数
        """
        colored_text = cls.colorize(text, color, bg_color, style)
        print(colored_text, **kwargs)

    @classmethod
    def preset(cls, text, preset_name, **kwargs):
        """
        使用预定义样式打印文本

        Args:
            text (str): 要打印的文本
            preset_name (str): 预定义样式名称，可选值见 PRESETS
            **kwargs: 其他传递给 print 函数的参数
        """
        if preset_name not in cls.PRESETS:
            raise ValueError(f"预设样式 '{preset_name}' 不存在")

        preset_config = cls.PRESETS[preset_name]
        colored_text = cls.colorize(
            text,
            color=preset_config.get("color"),
            bg_color=preset_config.get("bg_color"),
            style=preset_config.get("style"),
        )
        print(colored_text, **kwargs)

    # 快捷方法
    @classmethod
    def info(cls, text, **kwargs):
        """信息样式"""
        cls.preset(text, "info", **kwargs)

    @classmethod
    def success(cls, text, **kwargs):
        """成功样式"""
        cls.preset(text, "success", **kwargs)

    @classmethod
    def warning(cls, text, **kwargs):
        """警告样式"""
        cls.preset(text, "warning", **kwargs)

    @classmethod
    def error(cls, text, **kwargs):
        """错误样式"""
        cls.preset(text, "error", **kwargs)

    @classmethod
    def debug(cls, text, **kwargs):
        """调试样式"""
        cls.preset(text, "debug", **kwargs)

    @classmethod
    def highlight(cls, text, **kwargs):
        """高亮样式"""
        cls.preset(text, "highlight", **kwargs)


# 创建全局实例
tc = TerminalColor()


# 直接导出常用方法
def colorize(text, color=None, bg_color=None, style=None):
    """为文本添加颜色和样式"""
    return TerminalColor.colorize(text, color, bg_color, style)


def cprint(text, color=None, bg_color=None, style=None, **kwargs):
    """打印带颜色的文本"""
    TerminalColor.print(text, color, bg_color, style, **kwargs)


# 导出快捷方法
info = TerminalColor.info
success = TerminalColor.success
warning = TerminalColor.warning
error = TerminalColor.error
debug = TerminalColor.debug
highlight = TerminalColor.highlight

# 导出颜色常量
for color_name in TerminalColor.COLORS:
    globals()[color_name.upper()] = color_name

for bg_color_name in TerminalColor.BG_COLORS:
    globals()[bg_color_name.upper()] = bg_color_name

for style_name in TerminalColor.STYLES:
    globals()[style_name.upper()] = style_name

# 导出预设常量
for preset_name in TerminalColor.PRESETS:
    globals()[preset_name.upper()] = preset_name
